<!-- 异常费用列表 -->
<template>
  <el-dialog
    title="异常费用"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      ref="crud"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @row-del="rowDel"
    >
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        v-noMoreClick
        @click="submit"
        v-if="isSubmit && data.length > 0"
        >提 交</el-button
      >
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getExtraordinaryFee,
  extraordinaryFeeSaveOrUpdate,
  extraordinaryFeeRemove,
  extraordinaryFeeSubmit,
} from "@/api/transportation/demandorder";

export default {
  data() {
    return {
      dialogVisible: false,
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      data: [],
      id: "",
      alreadyExceptionBill: "0", //提交状态
      orderType: "",
    };
  },
  computed: {
    //是否已提交
    isSubmit() {
      return this.alreadyExceptionBill === "0";
    },
    option() {
      return {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        columnBtn: false,
        addBtn: this.isSubmit,
        menu: this.isSubmit,
        border: true,
        labelWidth: "180",
        column: [
          {
            label: "费用类型",
            prop: "costType",
            type: "select",
            props: {
              label: "costTypeDesc",
              value: "costType",
            },
            dicUrl: `/api/blade-tms/transportorder/TransportList/typesOfFee?orderType=${this.orderType}`,
            rules: [
              {
                required: true,
                message: "请选择费用类型",
                trigger: "change",
              },
            ],
          },
          {
            label: "费用",
            prop: "price",
            rules: [
              {
                required: true,
                message: "请输入费用",
                trigger: "blur",
              },
            ],
          },
          {
            label: "币别",
            prop: "currencyType",
            type: "select",
            props: {
              label: "currencyDesc",
              value: "name",
            },
            dicUrl: `/api/blade-tms/billing/Settlement/currency`,
            rules: [
              {
                required: true,
                message: "请选择币别",
                trigger: "change",
              },
            ],
          },
          {
            label: "费用凭证",
            prop: "file",
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传费用凭证",
                trigger: "change",
              },
            ],
          },
        ],
      };
    },
  },
  methods: {
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    refreshChange() {
      this.getList(this.page);
    },
    open(id, alreadyExceptionBill, orderType) {
      this.dialogVisible = true;
      this.id = id;
      this.orderType = orderType;
      this.alreadyExceptionBill = alreadyExceptionBill;
      this.getList(this.page);
    },
    getList(page) {
      this.loading = true;
      getExtraordinaryFee({
        current: page.currentPage,
        size: page.pageSize,
        id: this.id,
      }).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    rowSave(form, done, loading) {
      loading();
      extraordinaryFeeSaveOrUpdate({ ...form, exceptionInfoId: this.id }).then(
        ({ data }) => {
          if (data.code === 200 && data.success) {
            this.getList(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done(form);
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(form, index, done, loading) {
      loading();
      extraordinaryFeeSaveOrUpdate({ ...form, exceptionInfoId: this.id }).then(
        ({ data }) => {
          if (data.code === 200 && data.success) {
            this.getList(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done(form);
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(form) {
      this.$confirm("是否删除此异常费用?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          extraordinaryFeeRemove(form.id).then(({ data }) => {
            if (data.code === 200 && data.success) {
              this.getList(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {});
    },
    submit() {
      this.$confirm("提交后无法修改，是否确认提交异常费用?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          extraordinaryFeeSubmit({ id: this.id }).then(({ data }) => {
            if (data.code === 200 && data.success) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.$emit("refresh");
              this.dialogVisible = false;
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style></style>
